// pages/mine/repair/repairSubmit/repairSubmit.js
const app = getApp()
import util from "../../../../utils/util.js"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type:null,//报修分类 new重新提交  edit编辑  submit提交报修
    editId:null,//报修ID
    roomArr:[],//房源数据
    house_id:null,//房源ID
    house_type:null,//房源类型
    room_number: null,//房号
    tel: null,//联系电话
    title: null,//报修说明
    image: [],//添加图片
    note: null,//备注
    noteLength:0,
    showInput: false,

    roomShow: false,
    roomPicker: [],//房源房号
    roomCurrentIndex: 0,
    roomLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRooms()
    this.setData({
      editId: options.id || null,
      type: options.type || null,
    })
    if(options.type == 'submit'){
      wx.setNavigationBarTitle({
        title: '提交报修',
      })
    }else if(options.type == 'edit'){
      wx.setNavigationBarTitle({
        title: '编辑',
      })
      this.showEditInfo()
    }else if(options.type == 'new'){
      wx.setNavigationBarTitle({
        title: '重新提交',
      })
      this.showEditInfo()
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  /**
   * 数据更改
   */
  onChange(e) {
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail
    })
  },
  /**
   * textarea点击
   */
  changeTap(e){
    this.setData({
      showInput: e.currentTarget.dataset.type
    })
  },
  /**
   * textarea输入
   */
  changeInput(e) {
    this.setData({
      note: e.detail.value,
      noteLength: e.detail.value.length
    })
  },
  /**
   * textarea失去焦点
   */
  changeBlur(e){
    this.setData({
      showInput: e.currentTarget.dataset.type

    })
  },
  // 房源picker
  showRoomPicker() {
    this.setData({ roomShow: true })
    this.selectComponent("#roomPickerId").setValues([this.data.room_number])
  },
  roomCancel() {
    this.setData({ roomShow: false })
    this.selectComponent("#roomPickerId").setIndexes([this.data.roomCurrentIndex])
  },
  roomConfirm(event) {
    console.log(event)
    let arr = event.detail.value.keyId.split("_")
    console.log(arr)
    this.setData({
      roomShow: false,
      roomCurrentIndex: event.detail.index,
      room_number: event.detail.value.text,
      house_id: arr[0],
      house_type: arr[1],
    })
    this.selectComponent("#roomPickerId").setIndexes([this.data.roomCurrentIndex])
  },
  // 获取--点击报修显示房源
  getRooms() {
    this.setData({ roomLoading: true })
    app.globalData.request("get", "applet/repair/repairAdd", {
      id: wx.getStorageSync("id")
    })
      .then(res => {
        let arrs = res.data.housing
        arrs = arrs.map(item => {
          return {
            keyId: item.h_id + '_' + item.h_type,
            text: item.c_number
          }
        })
        console.log(arrs)
        this.setData({
          roomPicker: arrs,
          roomArr: arrs,
          tel: res.data.tel,
          roomLoading: false,
        })
      }).catch(err => {
        this.setData({ roomLoading: false })
        wx.showToast({
          title: err.msg,
        })
      })
  },
  // 提交确定
  submitItem(){
    if (!this.data.room_number) {
      wx.showToast({
        title: '请选择房号',
        icon: "none"
      })
    } else if (!this.data.title) {
      wx.showToast({
        title: '请输入报修说明',
        icon: "none"
      })
    } else if (!(/^1[3456789]\d{9}$/.test(this.data.tel))) {
      wx.showToast({
        title: '请正确输入联系电话',
        icon: "none"
      })
    } else {
      if (this.data.type == "edit" || this.data.type == "new") {
        this.repairsData()
      } else if (this.data.type == "submit") {
        app.globalData.request('post', "applet/repair/repairAdd", {
          user_id: wx.getStorageSync("id"),
          house_id: this.data.house_id,
          type: this.data.house_type,
          room_number: this.data.room_number,
          title: this.data.title,
          image: this.data.image,
          tel: this.data.tel,
          remark: this.data.note,
        })
          .then(res => {
            wx.showToast({
              title: res.msg,
            })
            setTimeout(_ => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          }).catch(err => {
            wx.showToast({
              title: err.mag,
              icon: "none"
            })
          })
      }
    }
  },
  // 报修编辑显示
  showEditInfo(){
    app.globalData.request("get","applet/repair/repairEdit",{
      repair_id: this.data.editId
    })
    .then(res => {
      console.log(res)
      // let index = this.getIndex(res.data.room_number)
      this.setData({
        room_number: res.data.room_number,
        tel: res.data.tenant_tel,
        title: res.data.title,
        note: res.data.remark,
        image: res.data.image,
        noteLength: res.data.remark.length,
      })
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon:"none"
      })
    })
  },
  // picker--获取索引
  // getIndex(item){
  //   let arr = this.data.roomArr
  //   let index = arr.findIndex(i => {
  //     return item == i.text
  //   })
  //   return index
  // },
  /**
   * 编辑和重新提交--点击确认
   */
  repairsData(){
    let url = ""
    if(this.data.type == "edit"){
      url = "applet/repair/repairEdit"
    }else if(this.data.type == "new"){
      url = "applet/repair/submission"
    }
    app.globalData.request("post",url,{
      repair_id:this.data.editId,
      title:this.data.title,
      tel:this.data.tel,
      remark:this.data.note,
      image:this.data.image
    })
    .then(res => {
      wx.showToast({
        title: res.msg,
      })
      setTimeout(_ => {
        wx.navigateBack({
          delta:1
        })
      },1000)
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon:"none"
      })
    })
  }
})